package On_class.L0420.book;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class BooksDaoJDBC implements BooksDao {
    @Override
    public void insert(Books books) {
        Books b=select(books.getId());
        Connection conn=null;
        PreparedStatement pstmt=null;
        if (b==null){
            try {
                conn=JDBCUtils.getConnection();
                String sql="insert into books values(?,?,?,?,?)";
                pstmt=conn.prepareStatement(sql);
                pstmt.setInt(1,books.getId());
                pstmt.setString(2,books.getName());
                pstmt.setDouble(3,books.getPrice());
                pstmt.setInt(4,books.getNumber());
                pstmt.setString(5,books.toString());
                int i=pstmt.executeUpdate();
                if (i>0){
                    System.out.println("保存成功");
                }else {
                    System.out.println("保存失败");
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                JDBCUtils.close(pstmt,conn);
            }
        }
    }

    @Override
    public void selectAll() {

    }

    @Override
    public Books select(int id) {
        return null;
    }

    @Override
    public void update(int id, Books b) {

    }

    @Override
    public void delete(int id) {

    }
}
